document.write('<script src="' + context
		+ '/app/gdzc/js/itemoptions.js"></script>');

function initPage() {
	var tb = new Ext.Toolbar();

	tb.id = 'ExtToolBar';

	var saveAction = new Ext.Action({
				id : 'saveButton',
				text : '<font class="oaFont" title="提交保存模板定义">提交保存</font>',
				handler : function() {
					saveTable('APP_GDZC_TEMPLATE,APP_GDZC_TEMPITEM',
							'single,multi', '-1,itemCount', function() {
								return checkit();
							}, function() {
								forwardFunc();
							});
				},
				iconCls : 'save'
			});

	var backAction = new Ext.Action({
		id : 'backAction',
		text : '<font class="oaFont" title="返回模板列表">返回</font>',
		handler : function() {
			document.location = context
					+ '/system/tablequery/jsplist.jsp?queryKey=gdzctemplist&typeId='
					+ $('TYPEID').value;
		},
		iconCls : 'back'
	});

	tb.add(saveAction);
	tb.add(backAction);
	// tb.add('-');

	tb.render('toolbar');

	initBasicItems();
}

function initBasicItems() {
	Ext.MessageBox.wait('数据初始化过程中...');
	Ext.Ajax.request({
		// 请求地址
		url : context + '/app/gdzctemplate.do?method=loaditem&tn=APP_GDZC_BASE',
		// 成功时回调
		success : function(response, options) {

			// 获取响应的json字符串
			Ext.MessageBox.hide();

			var json = response.responseText;
			var o = Ext.util.JSON.decode(json);

			if (o.totalCount > 0) {
				var tn = 'APP_GDZC_BASE';
				var items = o.columnList;
				for (var i = 0; i < items.length; i++) {
					var name = items[i].name;

					/*二维码图片附件暂不作为模板项*/
					if (name != 'GZCODEFILE') {

						var memo = items[i].memo;
						var tp = items[i].tp;

						var itemid = tn + '.' + name;

						str = '<table width="98%" border="0" align="center" id="'
								+ itemid
								+ '" idx="'
								+ c
								+ '" tn="'
								+ tn
								+ '" tnLabel="通用属性" columnName="'
								+ name
								+ '" tableName="'
								+ tn
								+ '" columnType="'
								+ tp
								+ '">';
						str += '<tr>';
						str += '<td width="10%" align="center">';
						str += '<img src="'
								+ context
								+ '/app/gdzc/images/edititem.png" alt="详细设置" style="cursor:hand;" onClick="javascript:editItem(\''
								+ itemid + '\', \'通用属性\');">';
						str += '</td>';
						str += '<td width="30%" align="left">&nbsp;显示名称<br/>'
						str += '&nbsp;<input style="width:90%" type="text" name="COLUMNLABEL_'
								+ c + '" value="' + memo + '">';
						str += '<input type="hidden" name="PROPKEY_' + c
								+ '" value="COMMON">';
						str += '<input type="hidden" name="PROPLABEL_' + c
								+ '" value="通用属性">';
						str += '<input type="hidden" name="COLUMNNAME_' + c
								+ '" value="' + name + '">';
						str += '<input type="hidden" name="COLUMNTYPE_' + c
								+ '" value="' + tp + '">';
						str += '<input type="hidden" name="TABLENNAME_' + c
								+ '" value="' + tn + '">';
						str += '<input type="hidden" name="COLUMNMEMO_' + c
								+ '" value="">';
						str += '<input type="hidden" name="COLUMNCHK1_' + c
								+ '" value="">';
						str += '<input type="hidden" name="COLUMNCHK2_' + c
								+ '" value="">';
						str += '<input type="hidden" name="COLUMNCHECK_' + c
								+ '" value="">';
						str += '<input type="hidden" name="COLUMNOTHER_' + c
								+ '" value="">';
						str += '</td>';
						str += '<td width="30%" align="left">&nbsp;默认值<br/>';
						str += '&nbsp;<input style="width:90%" type="text" name="COLUMNDVALUE_'
								+ c + '" value="">';
						str += '</td>';
						str += '<td width="30%" align="left">&nbsp;类型<br/>';
						str += '&nbsp;<select style="width:95%" name="ITEMTYPE_'
								+ c
								+ '">'
								+ getCommonItemOptions(tn, name, tp)
								+ '</select>';
						str += '</td>';
						str += '</tr>';
						str += '</table>';

						var o = $('DragContainer5');

						var bindObject = document.createElement("div");
						bindObject.id = "div_" + c;
						bindObject.setAttribute("_id", c);
						bindObject.className = "DragBox";

						bindObject.innerHTML = str;

						o.appendChild(bindObject);

						c++;

						CreateDragContainer(o);

					}
				}

				init(0);
			} else {
				Ext.Msg.alert('提示', '初始化通用属性失败！');
			}
		}
	});

}

function checkit() {
	var rnt = false;

	if (isblank($('TNAME'))) {
		alert('请填写模板名称');
	} else {
		var itemCount = getItemCount();

		createHidden('itemCount', itemCount);

		createHidden('USERID', userId);
		createHidden('USERNAME', userName);
		createHidden('GROUPID', groupId);
		createHidden('GROUPNAME', groupName);
		createHidden('TDATE', nowTime());

		rnt = true;
	}

	return rnt;
}

/**
 * 保存成功后回调函数
 */
function forwardFunc() {
	alert('模板保存成功');
	document.location = context
			+ '/system/tablequery/jsplist.jsp?queryKey=gdzctemplist&typeId='
			+ $('TYPEID').value;
}

/**
 * 
 * @param {}
 *            o
 */
function getItemCount() {
	var cnt = '';

	var cnt1 = $('DragContainer1').childNodes;
	var cnt2 = $('DragContainer2').childNodes;
	var cnt3 = $('DragContainer3').childNodes;
	var cnt4 = $('DragContainer4').childNodes;
	var cnt5 = $('DragContainer5').childNodes;

	for (var i = 0; i < cnt1.length; i++) {
		var tb = cnt1[i];
		var idx = tb.getAttribute('_id');

		cnt += idx + ',';
	}

	for (var i = 0; i < cnt2.length; i++) {
		var tb = cnt2[i];
		var idx = tb.getAttribute('_id');

		cnt += idx + ',';
	}

	for (var i = 0; i < cnt3.length; i++) {
		var tb = cnt3[i];
		var idx = tb.getAttribute('_id');

		cnt += idx + ',';
	}

	for (var i = 0; i < cnt4.length; i++) {
		var tb = cnt4[i];
		var idx = tb.getAttribute('_id');

		cnt += idx + ',';
	}

	for (var i = 0; i < cnt5.length; i++) {
		var tb = cnt5[i];
		var idx = tb.getAttribute('_id');

		cnt += idx + ',';
	}

	return cnt;
}

/**
 * tab 切换
 * 
 * @param {}
 *            name
 * @param {}
 *            cursel
 * @param {}
 *            n
 */
function setTab(name, cursel, n) {
	for (i = 1; i <= n; i++) {
		var menu = document.getElementById(name + i);
		var con = document.getElementById("con_" + name + "_" + i);
		menu.className = i == cursel ? "hover" : "";
		con.style.display = i == cursel ? "block" : "none";
		$('img' + i)
				? ($('img' + i).style.display = i == cursel ? "" : "none")
				: null;
	}
}